<%--
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
--%>
<%@ tag import="java.util.*" %>
<%@ include file="/taglibs.jsp" %>
<%@ attribute name="amount" required="true" type="java.lang.Long" %>
<%@ attribute name="stopAt" type="java.lang.String" %>

<%
  Map<String, Long> units = new LinkedHashMap<String, Long>();
  long millis = amount;
  long seconds = millis / 1000;
  millis -= (seconds * 1000);
  if (millis > 0) {
    units.put("millis", millis);
  }

  long minutes = seconds / 60;
  seconds -= (minutes * 60);
  if (seconds > 0) {
    units.put("seconds", seconds);
  }

  long hours = minutes / 60;
  minutes -= (hours * 60);
  if (minutes > 0) {
    units.put("minutes", minutes);
  }

  if (hours > 0) {
    units.put("hours", hours);
  }

  StringBuilder result = new StringBuilder();
  List<String> labelsToRender = new ArrayList<String>(units.keySet());
  Collections.reverse(labelsToRender);
  boolean keepGoing = true;
  for (String label : labelsToRender) {
    if (keepGoing) {
      Long value = units.get(label);
      result.append(' ');
      result.append(value);
      result.append(' ');
      result.append(value == 1 ? label.replaceAll("s$", "") : label);
    }
    if (label.equals(stopAt)) {
      keepGoing = false;
    }
  }
%>

<%=result%>